# _*_ coding: utf-8 _*_
from odoo import models, api, fields


class ProductTemplate(models.Model):
    _inherit = "product.product"

    product_purchase_limit_control = fields.Boolean(string="Product Purchase Limit Control")
    product_purchase_limit = fields.Float(string="Product Purchase Limit")

    _sql_constraints = [
        ('product_purchase_limit_must_greater_then_0',
         'check((not product_purchase_limit_control) or (product_purchase_limit_control and product_purchase_limit>0))',
         'Purchase limit must greater then 0 !')
    ]

    @api.model
    def create(self, vals):
        product_tmpl_id = vals.get("product_tmpl_id",None)
        if product_tmpl_id:
            product_tmpl = self.env["product.template"].browse(product_tmpl_id)
            vals["product_purchase_limit_control"] = product_tmpl.tmpl_purchase_limit_control
            vals["product_purchase_limit"] = product_tmpl.tmpl_purchase_limit
        return super(ProductTemplate, self).create(vals)
